package com.example.lianxi222.fragment.live;

import androidx.lifecycle.MutableLiveData;

import com.example.mvvm_base.base.model.BaseRepo;
import com.example.mvvm_base.userbean.FollowEntity;
import com.example.mvvm_base.userbean.GiftEntity;
import com.example.mvvm_base.userbean.LikeEntity;
import com.example.mvvm_base.userbean.Money;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class LiveRepo extends BaseRepo {

    public void follow(RequestBody requestBody, MutableLiveData<FollowEntity> success, MutableLiveData<String> failure){
        apiService.follow(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowEntity followEntity) {
                        success.setValue(followEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void cancelFollow(RequestBody requestBody, MutableLiveData<FollowEntity> success, MutableLiveData<String> failure){
        apiService.cancelFollow(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowEntity followEntity) {
                        success.setValue(followEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getGift(MutableLiveData<GiftEntity> success, MutableLiveData<String> failure){
        apiService.getGift()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GiftEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GiftEntity giftEntity) {
                     success.setValue(giftEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                      failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getMoney(String username, MutableLiveData<Money> success,MutableLiveData<String> failure){
       if( moneyDao.getMoney(username)!=null){
           success.setValue( moneyDao.getMoney(username));
       }else{
           failure.setValue("获取余额失败");
       }
    }
    public void updateMoney(Money money){
        moneyDao.updateMoney(money);
    }

}
